package com.lagou.test;

import com.lagou.proxy.CglibProxyFactory;
import com.lagou.proxy.JDKProxyFactory;
import com.lagou.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})
@Component
public class AccountServiceImplTest0 {

    @Autowired
    private AccountService accountService;
    @Autowired
    private  JDKProxyFactory jdkProxyFactory;
    @Autowired
    private CglibProxyFactory cglibProxyFactory;

    @Test
    public void testTransfer(){

        this.accountService.transfer("tom","jerry",100d);
    }

    /*
    * JDK动态代理优化转账案例
    * */
    @Test
    public void testTransferProxyJDK(){


        //当前返回的实际上是AccountService的代理对象proxy
        AccountService accountServiceJDKProxy = jdkProxyFactory.createAccountServiceJDKProxy();
        //代理对象proxy调用接口中的任意方法时,都会执行底层的invoke方法
        accountServiceJDKProxy.transfer("tom","jerry",100d);
    }


    /*
    * cglib动态代理优化转账案例
    * */
    @Test
    public void testTransferProxyCglib(){

        //当前返回的实际上是AccountService的代理对象proxy
        AccountService accountServiceCglibProxy = cglibProxyFactory.createAccountServiceCglibProxy();
        //代理对象proxy调用接口中的任意方法时,都会执行底层的invoke方法
        accountServiceCglibProxy.transfer("tom","jerry",100d);
    }
}
